package com.whc.common.design.handle.type;

import com.whc.common.Constant;
import com.whc.common.design.handle.colmun.ColumnHandle;
import com.whc.vo.GeneralColumn;
import com.whc.vo.Table;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wang-hai-cheng
 * @since 2022/1/1 20:03
 */
public class SaveChain extends TypeChain {

    @Override
    public Object handler(Map<String, Object> body) {
        if (!Constant.OP_TYPE.SAVE.equals(body.get(Constant.VARIABLE_NAME.TYPE))) {
            return handle.handler(body);
        }
        List<Table> tables = new ArrayList<>();
        body.forEach((k, v) -> {
            if (v instanceof ArrayList) {
                ((ArrayList<?>) v).forEach(lv -> {
                    Table table = new Table(k, new ArrayList<>());
                    if (lv instanceof LinkedHashMap) {
                        ((LinkedHashMap<String, Object>) lv).forEach((k1, v1) -> {
                            table.getColumns().add(new GeneralColumn(k1, v1));
                        });
                    }
                    ColumnHandle.handler(table);
                    tables.add(table);
                });
            }
        });
        return commonMapper.saveMany(tables);
    }
}